//
//  WelcomeBackViewController.swift
//  0730Microblog
//
//  Created by ding on 15/8/2.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeBackViewController: UIViewController {

    private var icomImgConstraint: NSLayoutConstraint?
    private var hConstant = UIScreen.mainScreen().bounds.height * CGFloat(0.25)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupWelcomeUI()
        
        if let urlString = UserAcountInfo.loadSharedUserAccountInfo()?.avatar_large {
            iconImg.sd_setImageWithURL(NSURL(string: urlString))
        }
//        iconImg.sd_setImageWithURL(NSURL(string: (UserAcountInfo.loadSharedUserAccountInfo()?.avatar_large)!))
//        icomImgConstraint?.constant = -hConstant * CGFloat(3)
//        // 动画代码给的位置不同 效果差别很大 甚至没有
//        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 8.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            print("dingcaozhi----donghua")
//            self.view.layoutIfNeeded()
//            }, completion: nil)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        icomImgConstraint?.constant = -hConstant * CGFloat(3)
        // 动画代码给的位置不同 效果差别很大 甚至没有
//        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 8.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
//            print("dingcaozhi----donghua")
//            self.view.layoutIfNeeded()
//            }, completion: { () -> Void in
//                 NSNotificationCenter.defaultCenter().postNotificationName(DINGSwitchRootViewControllerNotification, object: false)
//            })
        UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: CGFloat(0.8), initialSpringVelocity: CGFloat(8.0), options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            print("dingcaozhi----donghua")
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(DINGSwitchRootViewControllerNotification, object: true)
        }
       
    }
    
    private func setupWelcomeUI() {
        view.backgroundColor = UIColor.purpleColor()
        view.addSubview(backgroundImg)
        view.addSubview(iconImg)
        view.addSubview(welcomeLabel)
        
        // 自动布局
        backgroundImg.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: backgroundImg, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: backgroundImg, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: iconImg, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconImg, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        iconImg.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: iconImg, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: backgroundImg, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconImg, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -hConstant))
        // 错误写法
//        icomImgConstraint = iconImg.constraints.last
        // 'last'有点投机取巧
        icomImgConstraint = view.constraints.last
        
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImg, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImg, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
    }
    
    // 视图中的控件最好懒加载
    // 1> 背景图片
    private lazy var backgroundImg: UIImageView = {
        return UIImageView(image: UIImage(named: "ad_background"))
    }()
    // 2> 头像
    private lazy var iconImg: UIImageView = {
        let iconImg = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconImg.layer.cornerRadius = max(iconImg.frame.width, iconImg.frame.height) * CGFloat(0.5)
        iconImg.clipsToBounds = true
        return iconImg
    }()
    // 3> 标签
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢饮归来"
        return label
    }()

}
